---
title: "Spring MVC"
date: 2021-4-20
categories:
- java
- spring
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgba963c7">ResponseBodyAdvice 定制返回对象</a></li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgba963c7">
<h2 id="orgba963c7">ResponseBodyAdvice 定制返回对象</h2>
<div class="outline-text-2" id="text-orgba963c7">
<p>
Allows customizing the response after the execution of an @ResponseBody or a ResponseEntity controller method but before the body is written with an HttpMessageConverter.
Implementations may be registered directly with RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver or more likely annotated with @ControllerAdvice in which case they will be auto-detected by both.
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold;">public</span> <span style="font-weight: bold;">interface</span> <span style="font-weight: bold; text-decoration: underline;">ResponseBodyAdvice</span>&lt;<span style="font-weight: bold; text-decoration: underline;">T</span>&gt; {

        <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">         * Whether this component supports the given controller method return type</span>
<span style="font-style: italic;">         * and the selected </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@code HttpMessageConverter}</span><span style="font-style: italic;"> type.</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> returnType the return type</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> converterType the selected converter type</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@return</span><span style="font-style: italic;"> </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@code true}</span><span style="font-style: italic;"> if </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@link #beforeBodyWrite}</span><span style="font-style: italic;"> should be invoked;</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@code false}</span><span style="font-style: italic;"> otherwise</span>
<span style="font-style: italic;">         */</span>
        <span style="font-weight: bold; text-decoration: underline;">boolean</span> <span style="font-weight: bold;">supports</span>(<span style="font-weight: bold; text-decoration: underline;">MethodParameter</span> <span style="font-weight: bold; font-style: italic;">returnType</span>, <span style="font-weight: bold; text-decoration: underline;">Class</span>&lt;? <span style="font-weight: bold;">extends</span> <span style="font-weight: bold; text-decoration: underline;">HttpMessageConverter</span>&lt;?&gt;&gt; <span style="font-weight: bold; font-style: italic;">converterType</span>);

        <span style="font-style: italic;">/**</span>
<span style="font-style: italic;">         * Invoked after an </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">{@code HttpMessageConverter}</span><span style="font-style: italic;"> is selected and just before</span>
<span style="font-style: italic;">         * its write method is invoked.</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> body the body to be written</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> returnType the return type of the controller method</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> selectedContentType the content type selected through content negotiation</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> selectedConverterType the converter type selected to write to the response</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> request the current request</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@param</span><span style="font-style: italic;"> response the current response</span>
<span style="font-style: italic;">         * </span><span style="font-weight: bold; font-style: italic; text-decoration: underline;">@return</span><span style="font-style: italic;"> the body that was passed in or a modified (possibly new) instance</span>
<span style="font-style: italic;">         */</span>
        <span style="font-weight: bold; text-decoration: underline;">@Nullable</span>
        <span style="font-weight: bold; text-decoration: underline;">T</span> <span style="font-weight: bold;">beforeBodyWrite</span>(<span style="font-weight: bold; text-decoration: underline;">@Nullable</span> <span style="font-weight: bold; text-decoration: underline;">T</span> <span style="font-weight: bold; font-style: italic;">body</span>, <span style="font-weight: bold; text-decoration: underline;">MethodParameter</span> <span style="font-weight: bold; font-style: italic;">returnType</span>, <span style="font-weight: bold; text-decoration: underline;">MediaType</span> <span style="font-weight: bold; font-style: italic;">selectedContentType</span>,
                        <span style="font-weight: bold; text-decoration: underline;">Class</span>&lt;? <span style="font-weight: bold;">extends</span> <span style="font-weight: bold; text-decoration: underline;">HttpMessageConverter</span>&lt;?&gt;&gt; <span style="font-weight: bold; font-style: italic;">selectedConverterType</span>,
                        <span style="font-weight: bold; text-decoration: underline;">ServerHttpRequest</span> <span style="font-weight: bold; font-style: italic;">request</span>, <span style="font-weight: bold; text-decoration: underline;">ServerHttpResponse</span> <span style="font-weight: bold; font-style: italic;">response</span>);

}
</pre>
</div>
<p>
使用示例：
</p>
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold; text-decoration: underline;">@ControllerAdvice</span>
<span style="font-weight: bold;">public</span> <span style="font-weight: bold;">class</span> <span style="font-weight: bold; text-decoration: underline;">ResponseResultHandler</span> <span style="font-weight: bold;">implements</span> <span style="font-weight: bold; text-decoration: underline;">ResponseBodyAdvice</span>&lt;<span style="font-weight: bold; text-decoration: underline;">Object</span>&gt; {

    <span style="font-weight: bold;">public</span> <span style="font-weight: bold; text-decoration: underline;">Object</span> <span style="font-weight: bold;">beforeBodyWrite</span>(<span style="font-weight: bold; text-decoration: underline;">Object</span> <span style="font-weight: bold; font-style: italic;">body</span>, <span style="font-weight: bold; text-decoration: underline;">MethodParameter</span> <span style="font-weight: bold; font-style: italic;">methodParameter</span>, <span style="font-weight: bold; text-decoration: underline;">MediaType</span> <span style="font-weight: bold; font-style: italic;">mediaType</span>,
                                  <span style="font-weight: bold; text-decoration: underline;">Class</span>&lt;? <span style="font-weight: bold;">extends</span> <span style="font-weight: bold; text-decoration: underline;">HttpMessageConverter</span>&lt;?&gt;&gt; <span style="font-weight: bold; font-style: italic;">aClass</span>,
                                  <span style="font-weight: bold; text-decoration: underline;">ServerHttpRequest</span> <span style="font-weight: bold; font-style: italic;">serverHttpRequest</span>, <span style="font-weight: bold; text-decoration: underline;">ServerHttpResponse</span> <span style="font-weight: bold; font-style: italic;">serverHttpResponse</span>) {

        <span style="font-weight: bold; text-decoration: underline;">Map</span>&lt;<span style="font-weight: bold; text-decoration: underline;">String</span>, <span style="font-weight: bold; text-decoration: underline;">Object</span>&gt; <span style="font-weight: bold; font-style: italic;">result</span> = <span style="font-weight: bold;">new</span> <span style="font-weight: bold; text-decoration: underline;">HashMap</span>&lt;&gt;();
        result.put(<span style="font-style: italic;">"code"</span>, xxx);
        result.put(<span style="font-style: italic;">"msg"</span>, xxx);
        result.put(<span style="font-style: italic;">"data"</span>, body);
        <span style="font-weight: bold;">return</span> result;
    }

}
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2021-4-20</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
